﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using App.Data;
using System.Data;

/// <summary>
/// Summary description for BindDropdownList
/// </summary>
public class BindDropdownList
{    
    /// <summary>
    /// Binds a Dropdownlist to a DataTable
    /// </summary>
    /// <param name="ddl"></param>
    /// <param name="dt"></param>
    /// <param name="displayField"></param>
    /// <param name="valueField"></param>
    public static void BindDropDownList(DropDownList ddl, DataTable dt, String displayField, String valueField)
    {

        if (dt == null || dt.Rows.Count <= 0)
        {
            if (ddl.Items.Count > 0)
                ddl.Items.Clear();
            ddl.DataSource = null;
            ddl.DataBind();
        }
        else
        {
            ddl.DataSource = dt.DefaultView;
            ddl.DataTextField = displayField;
            ddl.DataValueField = valueField;
            ddl.DataBind();
        }
        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));

    }
    /// <summary>
    /// Binds a Dropdownlist Controls to a Collection
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ddl"></param>
    /// <param name="entities"></param>
    /// <param name="displayField"></param>
    /// <param name="valueField"></param>
    public static void BindToList<T>(DropDownList ddl, IList<T> entities, String displayField, String valueField)
        where T : System.Data.Objects.DataClasses.EntityObject
    {
        if (entities == null || entities.Count == 0)
        {
            if (ddl.Items.Count > 0)
                ddl.Items.Clear();
            ddl.DataSource = null;
        }
        else
        {
            ddl.DataSource = entities;
            ddl.DataTextField = displayField;
            ddl.DataValueField = valueField;
        }
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
    }

    public static void Countries(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var countries = from P in context.Countries orderby P.Name select P;
        BindToList(ddl, countries == null ? null : countries.ToList(), "Name", "ID");
    }
    public static void Cities(DropDownList ddl, int countryID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var cities = from P in context.Cities where P.CountryID == countryID select P;
        BindToList(ddl, cities == null ? null : cities.ToArray(), "Name", "ID");
    }

    public static void ProjectStatuses(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var statuses = from P in context.ProjectStatuses select P;
        BindToList(ddl, statuses == null ? null : statuses.ToArray(), "Name", "ID");
    }
    public static void PropertyTypes(DropDownList ddl, int businessTypeID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var types = from P in context.PropertyTypes where P.BusinessTypeID == businessTypeID && P.ID != ConfigReader.OtherPropertyTypeID select P;
        BindToList(ddl, types == null ? null : types.ToArray(), "Name", "ID");
    }

    public static void BindProjectFileTypes(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var types = from P in context.ProjectFileTypes select P;
        BindToList(ddl, types == null ? null : types.ToArray(), "Name", "ID");
    }

    public static void Zones(DropDownList ddl, int cityID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var zones = from P in context.Zones where P.CityID == cityID select P;
        BindToList(ddl, zones == null ? null : zones.ToArray(), "Name", "ID");
    }

    public static void Designations(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var designations = from P in context.Designations select P;
        BindToList(ddl, designations == null ? null : designations.ToList(), "Name", "ID");
    }

    public static void Currencies(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var currencies = from P in context.Currencies select P;
        BindToList(ddl, currencies == null ? null : currencies.ToList(), "Name", "ID");
    }
}